package com.lxl.chat.netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @describe: webSocket服务启动类
 * @author: lxl
 * @date: 2018/11/8 22:53
 * @version: v1.0
 */
@Slf4j
@Component
public class WebSocketServer {

    private ServerBootstrap serverBootstrap;

    private WebSocketServer() {
        NioEventLoopGroup boss = new NioEventLoopGroup();
        NioEventLoopGroup worker = new NioEventLoopGroup();
        serverBootstrap = new ServerBootstrap();
        serverBootstrap.group(boss, worker).channel(NioServerSocketChannel.class).childHandler(new WebSocketInitializer());
    }

    public static WebSocketServer getInstance() {
        return SingletonWebSocketServer.INSTANCE;
    }

    public void start() throws InterruptedException {
        serverBootstrap.bind(8088).sync();
        //log.warn("---- netty webSocket started ^_^ ---- ");
    }

    public static class SingletonWebSocketServer {
        static final WebSocketServer INSTANCE = new WebSocketServer();
    }

}
